/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements.  See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


/*************************************************************************
 *\author: starlove
 *************************************************************************/
#ifndef __CC_PYTHON_IMPORT_O_SCHEDULER_IMPL__
#define __CC_PYTHON_IMPORT_O_SCHEDULER_IMPL__

#include "python_object_scheduler.h"

NS_CCPY_BEGIN	
	CC_OBJECT_WRAPPER_IMPL_NOUPDATE(CCTimer, CPyTimer)
	void __CCTimerWrapper::update(float dt)
	{
		cocos2d::CCTimer::update(dt);
	}

	void __CCTimerWrapper::sel_callback(float dt)
	{
		if(m_pyObject)
		{
			CPyTimer *timer = (CPyTimer *)m_pyObject;
			timer->callback(dt);
		}
	}
	
	PYCLASS_INIT_AND_BASE(CPyTimer, CPyCCObject, "CCTimer", "Python CCTimer Object");
	PYCLASS_BEGIN_METHOD_DEF(CPyTimer)
	PYCLASS_ADD_METHOD(CPyTimer, update)
	PYCLASS_END_METHOD_DEF(CPyTimer)
	PYCLASS_BEGIN_GETSET_DEF(CPyTimer)
	PYCLASS_ADD_GETSET(CPyTimer, Interval)
	PYCLASS_ADD_GETSET(CPyTimer, Selector)
	PYCLASS_END_GETSET_DEF(CPyTimer)
	PYCLASS_IMP_INTERFACE_NEW(CPyTimer)
	
	void CPyTimer::init_callback()
	{
		m_fn = NULL;
	}

	void CPyTimer::set_callback(PyObject *fn)
	{
		Py_XDECREF(m_fn);
		m_fn = fn;
		Py_XINCREF(m_fn);
	}
		
	PyObject *CPyTimer::get_callback()
	{
		if(!m_fn)
		{
			Py_RETURN_NONE;
		}
		return m_fn;
	}

	void CPyTimer::callback(float dt)
	{
		if(m_fn)
			PyObject_Call(m_fn, PyTuple_Pack(1, PyFloat_FromDouble(dt)), NULL);
	}

	int CPyTimer::__init__(CPyTimer *pySelf, PyObject *args, PyObject *kwds)
	{
		if( !args )
			return 0;
		static char* kwlist[] = {"selector", "fSeconds", "nRepeat", "fDelay", NULL};
		PyObject *pyFn = NULL;
		float fSeconds = 0;
		unsigned int nRepeat = 0;
		float fDelay = 0;
		if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OfIf", kwlist, &pyFn, &fSeconds, &nRepeat, &fDelay))
			return 0;
		__CCTimerWrapper *wrap = dynamic_cast<__CCTimerWrapper *>(pySelf->getCCObject());
		if(!wrap)
			return 0;
		pySelf->init_callback();
		if(pyFn && PyCallable_Check(pyFn))
			pySelf->set_callback(pyFn);
		wrap->retain();
		if(fSeconds > 0)
		{
			wrap->initWithTarget(wrap, __schedule_selector(__CCTimerWrapper::sel_callback), fSeconds, nRepeat, fDelay);
		}else
		{
			wrap->initWithTarget(wrap, __schedule_selector(__CCTimerWrapper::sel_callback));
		}
		return 0;
	}

	void CPyTimer::__del__(CPyTimer *self)
	{
		self->set_callback(NULL);
		self->ob_type->tp_free(self);
	}
	
	PYCLASS_GET_IMPL(CPyTimer, Interval)
	{
		__CCTimerWrapper *wrap = dynamic_cast<__CCTimerWrapper *>(pySelf->getCCObject());
		if(!wrap)
			Py_RETURN_NONE;
		return Py_BuildValue("f", wrap->getInterval());
	}
	
	PYCLASS_SET_IMPL(CPyTimer, Interval)
	{
		__CCTimerWrapper *wrap = dynamic_cast<__CCTimerWrapper *>(pySelf->getCCObject());
		if(wrap)
			wrap->setInterval((float)PyFloat_AsDouble(pyValue));
		return 0;
	}
	
	PYCLASS_GET_IMPL(CPyTimer, Selector)
	{
		return pySelf->get_callback();
	}
	
	PYCLASS_SET_IMPL(CPyTimer, Selector)
	{
		if(PyCallable_Check(pyValue))
			pySelf->set_callback(pyValue);
		return 0;
	}
	
	PYCLASS_METHOD_IMPL(CPyTimer, update)
	{
		float f = 0;
		if( !PyArg_ParseTuple(pyArgs, "f", &f) )
			Py_RETURN_NONE;
		__CCTimerWrapper *wrap = dynamic_cast<__CCTimerWrapper *>(pySelf->getCCObject());
		if(wrap)
			wrap->update(f);
		Py_RETURN_NONE;
	}

NS_CCPY_END

#endif